package com.iwanghang.movelineview.whCustomView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import com.iwanghang.movelineview.whUtil.Tools;

public class MoveFoldLineView extends View{

    // 画笔
    private Paint mLinePaint;
    private Path path;
    private float nowX;
    private float nowY;
    // 要绘制的线，组合这个线的点的集合
    private Point[] mPoints;

    public MoveFoldLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MoveFoldLineView(Activity activity) {
        super(activity);
        initMoveLinePaint();
    }

    public MoveFoldLineView(Activity activity, float startX, float startY, float endX, float endY) {
        super(activity);
        initMoveLinePaint();
    }

    public MoveFoldLineView(Activity activity, Point[] mPoints) {
        super(activity);
        initMoveLinePaint();
        this.mPoints = mPoints;
        Tools.logByWh("MoveFoldLineView init X = " + mPoints[0].x + " - Y = " + mPoints[0].y);
    }

    private void initMoveLinePaint() {
        if(mLinePaint == null){
            mLinePaint = new Paint();
        }
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(Color.rgb(221, 203, 235));
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(5);

        path = new Path();
    }

    // 当Android系统需要绘制一个View对象时，就会嗲用该对象的onDraw
    /**移动的速度，越大 移动越快*/
    private int speed = 1;
    @Override
    protected void onDraw(Canvas canvas) {
        Tools.logByWh("MoveFoldLineView nowPointPosition = " + nowPointPosition);
        if (nowPointPosition == 0){
            nowX = mPoints[0].x;
            nowY = mPoints[0].y;
            path.moveTo(nowX, nowY);
        } else {
            path.lineTo(nowX, nowY);
        }
        Tools.logByWh("MoveFoldLineView nowX = " + nowX + " - nowY = " + nowY);
        canvas.drawPath(path, mLinePaint);
        nowPointPosition = nowPointPosition + speed;

//        for (int i = 0; i < mPoints.length; i++) {
//            Point mPoint = mPoints[i];
//            float x = mPoint.x;
//            float y = mPoint.y;
//            if(i == 0){
//                Tools.logByWh("moveTo x = " + x + " - y = " + y);
//                path.moveTo(x,y);
//            }else {
//                Tools.logByWh("lineTo x = " + x + " - y = " + y);
//                path.lineTo(x,y);
//            }
//        }
//        canvas.drawPath(path,mLinePaint);

    }

    /**启动动画*/
    public void StartAnimationRunnable(){
        Tools.logByWh("StartAnimationRunnable");
        this.nowPointPosition = 0;
        postDelayed(new AnimationRunnable(),1);
    }

    /**目前要绘制的线数量*/
    private int nowPointPosition;
    private class AnimationRunnable implements Runnable{
        @Override
        public void run() {
            Tools.logByWh("AnimationRunnable run");
            if(mPoints!=null) {
                if (mPoints.length > nowPointPosition) {
                    Point mPoint = mPoints[nowPointPosition];
                    if (mPoint != null){
                        nowX = mPoint.x;
                        nowY = mPoint.y;
                    } else {
                        nowX = 0;
                        nowY = 0;
                    }
//                    nowX = mPoint.x;
//                    nowY = mPoint.y;
                    Tools.logByWh("AnimationRunnable nowX = " + nowX + " - nowY = " + nowY);
                    postDelayed(this, 1);
                    postInvalidate();
                }
            }
        }
    }

}
